$().ready(function(){
    let page = {};
    function init(){
        //获取普通类别
        getClassList(1,'#pt',function(res){
            page.class_pt = res;
        });
        //获取疾病类别
        getClassList(2,'#jp',function (res) {
            page.class_jb = res;
        });
    }
    init();

    //新增，修改
    $('#submit_class').on('click',function(){
       let type = $('#class_type').val();
       let id = $('#class_id').val();
       operationClass(type,id);
    });
    //删除
    $('.class-box').on('click','.class-del',function(){
        let class_id = $(this).data('id');
        operationClass(3,class_id);
    });
    //获取分类
    function getClassList(type,selector,callback){
        //type 1普通  2疾病
        getAjax(`${global.apiUrl}/get_community_service_kitchen_class`,res=>{
            let html = '';
            callback(res);
            for(let i in res){
                html += `
                    <div class="class-item themeBorder">
                        <div class="class-icon themeBgColor" data-toggle="modal" data-target="#classModal" data-id="${res[i].id}" data-type="2" data-status="${type}"><i class="fa fa-tags"></i></div>
                        <div class="class-text themeColor">${res[i].class_name}</div>
                        <div class="class-del themeBgColor" data-id="${res[i].id}">删除</div>
                    </div>
                `
            }
            html += `
                <div class="class-item themeBorder" id="add_btn" data-toggle="modal" data-target="#classModal" data-type="1" data-status="${type}">
                    <div class="class-icon"><i class="fa fa-plus" style="color:#0b67ff"></i></div>
                    <div class="class-text themeBgColor" style="color:#fff;">添加类别</div>
                    <div class="class-icon"><i class="fa fa-plus themeColor" style="color:#0b67ff"></i></div>
                </div>
            `
            $(selector).html(html);
        },{
            type:type,
            kitchen_id:global.kitchen_id
        })
    }
    //modal on show init
    $('#classModal').on('show.bs.modal',function(event){
        let modal = $(this);
        let btn = $(event.relatedTarget);
        let type = btn.data('type');//type 1添加  2修改
        let status = btn.data('status');//1 普通  2 疾病
        //modal信息清空
        modal.find('#class_name').val('');
        modal.find('#class_type').val(type);
        modal.find('#class_id').val('');
        modal.find('#class_status').val(status);
        if(type == 1){
            modal.find('#classModalLabel').text('添加新分类');
            modal.find('#class_status').val(status);
        }
        if(type == 2){
            modal.find('#classModalLabel').text('修改分类');
            let id = btn.data('id');
            modal.find('#class_id').val(id);
            let class_list = [];
            if(status == 1){
                class_list = page.class_pt;
            }
            if(status == 2){
                class_list = page.class_jb;
            }
            for(let i in class_list){
                if(class_list[i].id == id){
                    modal.find('#class_name').val(class_list[i].class_name);
                }
            }
        }
    })
    //分类操作
    function operationClass(type,id){
        //type 1 新增   2修改   3删除
        let class_id = id;
        let class_name = '';
        let status = $('#class_status').val();
        let pro = {};
        if(type == 1||type == 2){
            class_name = $('#class_name').val();
            pro = {
                id:class_id,
                class_name:class_name,
                kitchen_id: global.kitchen_id,
                community_id:global.community_id,
                type:status
            }
            pro = JSON.stringify(pro);
        }
        if(type == 3){
            console.log(111);
            let cf = confirm('是否确认删除该分类？');
            if(cf){

            }else{
                return
            }
        }
        getAjax(`${global.apiUrl}/admin_operation_community_service_kitchen_class`,res=>{
            // console.log(res);
            if(res==1){
                alert('操作已完成！');
                if(type == 1||type == 2){
                    $('#classModal').modal('hide');
                }
                //获取普通类别
                getClassList(1,'#pt',function(res){
                    page.class_pt = res;
                });
                //获取疾病类别
                getClassList(2,'#jp',function (res) {
                    page.class_jb = res;
                });
            }
        },{
            type: type,
            pro:pro,
            id:class_id
        })

    }
});
